package fr.grassaud.hmvc.event;

/**
 * Event send by components (View, Model or Controller) 
 * @author vgrassaud
 *
 */
public class HMVCEvent {
    
    /**
     * Types of the event
     * @author vgrassaud
     *
     */
    public enum EventType {
	MODEL, VIEW, CONTROLLER
    }
    
    /**
     * The sender of the event
     */
    private Object source;
    /**
     * The name of the event, this name should be unique
     */
    private String name;
    /**
     * The type of the event
     */
    private EventType eventType;

    /**
     * Creates an event with the given name and the given sender. The type if the event will be <code>EventType.CONTROLLER</code>
     * @param name the name of the event
     * @param source the sender of the event
     */
    public HMVCEvent(String name, Object source) {
	this.source = source;
	this.name = name;
	eventType = EventType.CONTROLLER;
    }

    
    /**
     * Creates an event with the given name, given sender and given type. 
     * @param name the name of the event 
     * @param source the sender of the event
     * @param type the type of the event
     */
    public HMVCEvent(String name, Object source, EventType type) {
	this(name, source);
	setEventType(type);
    }

    /**
     * Returns the sender of the event, should an object annotated with <code>Model, View or Controller</code>
     * @return the sender of the event
     */
    public Object getSource() {
	return this.source;
    }

    /**
     * Returns the name of the event
     * @return the name of the event
     */
    public String getName() {
	return this.name;
    }

    
    /**
     * Returns the parameters of this event. This methods is used by the <code>HMVCSession</code> to invoke the method
     * using parameters. By default, the parameter are the event itself so you can write your method in your controller like that 
     * <code>
     * <pre>
     * @OnViewEvent(name=AttachEvent.NAME) 
     * protected void attachComponent(AttachEvent event) {
     *   // do something
     * }
     *   
     * 
     * </pre>
     * </code>
     * @return the parameters of this event used during invocation of the method that managed this event 
     */
    public Object[] getParameters() {
	return new Object[] { this };
    }

    /**
     * Returns the type of the event.
     * The type of an event is used by the <code>HMVCSession</code> in order to assure the use of the HMVC pattern. 
     * A view can only send a event with a type <code>EventType.VIEW</code>, a model can only send an event with a type 
     * <code>EventType.MODEL</code>
     * @return the type of the event
     */
    public EventType getEventType() {
	return this.eventType;
    }

    /**
     * Sets the type of the event. Don't use this method, it' used by the framework 
     * @param type the type of the event
     */
    public void setEventType(EventType type) {
	this.eventType = type;
    }

}
